﻿<html>
<head>
<script>	
var bookmarkTreeNodes,i,
	linkUrl = 'javascript:void((function(w,d){if(w.__NS__JIPIN&&typeof%20w.__NS__JIPIN.showPinBoard  === "function"){w.__NS__JIPIN.showPinBoard();return}var%20e=d.createElement("script");e.setAttribute("type","text/javascript");e.setAttribute("charset","UTF-8");e.setAttribute("src","http://apitu.jipin.kaixin001.com/remote/assistant?jsonp=showpinboard&&t="+(+new%20Date));e.setAttribute("id","__kx__script");(d.getElementsByTagName("head")[0]||d.body).appendChild(e)})(window,document));',
	properties={},windowID;

/*
	查找返回结果处理
*/
function searchResult(bookmarkTreeNodes) {
	//console.log("搜索返回结果如下: "+bookmarkTreeNodes);
	if(bookmarkTreeNodes.length > 0)
	{
		//delete old bookmarks
		for(i=0;i<bookmarkTreeNodes.length;i++)
			chrome.bookmarks.remove(bookmarkTreeNodes[i].id);
	}
	
	/*
		获取所有的收藏夹书签,然后创建书签 
	*/
	chrome.bookmarks.getTree(function(bookmarkTreeNodes) {		
		chrome.bookmarks.create({parentId: bookmarkTreeNodes[0].children[0].id,
	  		title:"✚ 开心集品", url: linkUrl},function() {
	       		//console.log("创建书签成功");
	    });		                   	
	});
	
	/*
		查询下载页面
	*/
	chrome.tabs.getAllInWindow(windowID,function(tabs) {
		console.log(tabs);
		var i,url;
		var code,details={},bGot;
		bGot = false;
		
		properties.url='http://jipin.kaixin001.com/navi/plug.php?restart=true';
		properties.selected = true;
		//properties.pinned = false;
		
		for(i=0;i<tabs.length;i++) {
			url=tabs[i].url;
			if(url.match('http://tu.kxjie.com/navi') != null) {
				chrome.tabs.remove(tabs[i].id);
				if(bGot == false) {
					chrome.tabs.create(properties,function(tab) {
						//console.log(tab);
					});
					bGot = true;
				}
			}
		}
		if(bGot == false) { 
			chrome.tabs.create(properties,function(tab) {
			});
		}
	});
}

var firstRun = (localStorage['firstRun'] == 'true');
if (!firstRun) {
  	localStorage['firstRun'] = 'true';
	//搜索集品助手标签
	chrome.bookmarks.search("✚ 开心集品",searchResult);
	properties.url='http://jipin.kaixin001.com/navi/plug.php?chrome_restart=true';
	properties.selected = true;
}
</script>
</head>
</html>